local danjin = fk.CreateSkill{
  name = "hanqing__danjin",
}

Fk:loadTranslationTable{
  ["hanqing__danjin"] = "胆进",
  [":hanqing__danjin"] = "若你使用的上一张牌造成过伤害或令角色回复过体力，你可以视为使用【出其不意】，且你与目标同时可以将一张牌置为底牌。",

  ["#hanqing__danjin"] = "胆进：你可以视为使用【出其不意】，你与目标可以将一张牌置为底牌",
  ["#hanqing__danjin-ask"] = "胆进：%src 使用【出其不意】，是否将一张牌置为底牌？",
}

danjin:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "unexpectation",
  prompt = "#hanqing__danjin",
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if #cards ~= 0 then return end
    local c = Fk:cloneCard("unexpectation")
    c.skillName = danjin.name
    return c
  end,
  before_use = function (self, player, use)
    local room = player.room
    local targets = {player}
    table.insertTableIfNeed(targets, use.tos)
    targets = table.filter(targets, function (p)
      return not p:isNude()
    end)
    if #targets > 0 then
      room:playCardEmotionAndSound(player, Fk:cloneCard("unexpectation"))
      room:doIndicate(player, use.tos)
      local result = room:askToJointCards(player, {
        players = targets,
        min_num = 1,
        max_num = 1,
        include_equip = true,
        cancelable = true,
        skill_name = danjin.name,
        prompt = "#hanqing__danjin-ask:"..player.id,
      })
      for _, ids in pairs(result) do
        if #ids > 0 then
          use.card:addSubcards(ids)
        end
      end
    end
  end,
  enabled_at_play = function (self, player)
    return player:getMark(danjin.name) > 0
  end,
  enabled_at_response = function (self, player, response)
    return not response and player:getMark(danjin.name) > 0
  end
})

danjin:addEffect(fk.CardUseFinished, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(danjin.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if data.damageDealt then
      room:setPlayerMark(player, danjin.name, 1)
      return
    else
      local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
      if use_event and #use_event:searchEvents(GameEvent.Recover, 1, function (e)
          return e.data.card == data.card
        end) > 0 then
        room:setPlayerMark(player, danjin.name, 1)
        return
      end
    end
    room:setPlayerMark(player, danjin.name, 0)
  end,
})

danjin:addAcquireEffect(function (self, player, is_start)
  if not is_start then
    local room = player.room
    room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
      local use = e.data
      if use.from == player then
        if use.damageDealt then
          room:setPlayerMark(player, danjin.name, 1)
          return true
        else
          if #e:searchEvents(GameEvent.Recover, 1, function (e2)
              return e2.data.card == use.card
            end) > 0 then
            room:setPlayerMark(player, danjin.name, 1)
            return true
          end
        end
        room:setPlayerMark(player, danjin.name, 0)
        return true
      end
    end, nil, Player.HistoryGame)
  end
end)

danjin:addAI(nil, "vs_skill")

return danjin
